LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-08-2008, 12:32 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 10.04, Crunchbang Statler
Posts: 3,325

Rep: Reputation: 168Reputation: 168
[bash]difference between script on command line and in file


Hi,

have to write a script; on the command line, it's (as one-liner):
Code:
time while read line; do echo -n "$line "; grep -P -m 1 "\t[A-Z]$line" logfile |wc -l; done < <(tail db.txt)
The time is only for debugging as is the tail and the script works

If I put it in a file
Code:
while read line;
do
    echo -n "$line ";
    grep -P -m 1 "\t[A-Z]$line" logfile |wc -l;
done < <(tail db.txt)
and execute with sh verify.sh, it complains about the last line
Code:
wim@btd-techweb01:~/log$ sh verify.sh
verify.sh: line 5: syntax error near unexpected token `<'
verify.sh: line 5: `done < <(tail db.txt)'
Can someone explain to me why ? I assume that the input redirection has to be outside the file, but it does not explain the why.

If I add '#!/bin/bash' at the beginning of verify.sh and make it executable, it works (again) as expected.

Note:
I will probably post some more questions as it needs some serious optimisation and a slight modification.
 
Old 09-08-2008, 12:56 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
Quote:
If I add '#!/bin/bash' at the beginning of verify.sh and make it executable, it works (again) as expected.
Quote:
sh verify.sh
I suspect your 'sh' is the POSIX shell and is NOT symlinked (instead) to bash. You should never assume it is. If you want bash, specify it explicitly (as you later did).
 
Old 09-08-2008, 01:04 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 10.04, Crunchbang Statler
Posts: 3,325

Original Poster
Rep: Reputation: 168Reputation: 168
Hi Chris,

thanks for the reply and the warning never to assume.

Code:
wim@btd-techweb01:~$ which sh
/bin/sh
wim@btd-techweb01:~$ ls -l /bin/sh
lrwxrwxrwx  1 root root 4 2006-04-19 14:29 /bin/sh -> bash*
wim@btd-techweb01:~$ ls -l /bin/bash
-rwxr-xr-x  1 root bin 666452 2004-11-04 00:30 /bin/bash*
wim@btd-techweb01:~$
Definitely symlinked to bash; any other thoughts?

Last edited by Wim Sturkenboom; 09-08-2008 at 01:06 AM.
 
Old 09-08-2008, 01:56 AM   #4
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
If bash is started as sh, it will enable posix behavior.

Try /bin/bash verify.sh instead. The error will go away.

Last edited by jschiwal; 09-08-2008 at 01:57 AM.
 
Old 09-08-2008, 02:20 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 10.04, Crunchbang Statler
Posts: 3,325

Original Poster
Rep: Reputation: 168Reputation: 168
Thank you, that indeed worked as expected.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 48 Yesterday 11:13 PM
bash script 'for each command line argument' true_atlantis Linux - Newbie 3 01-28-2009 01:51 PM
bash script to find the difference between the 4th element of each line in 2 files dontbugmeplz Programming 6 07-21-2008 08:05 AM
bash command for make difference between two txt file kkpal Linux - Newbie 7 05-12-2008 07:07 AM
Bash Script, no new line for echo command jorisb Linux - General 5 11-05-2005 12:08 AM


All times are GMT -5. The time now is 02:45 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration